package com.ysd.Dao.Impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.News;
import com.ysd.entity.NewsClassiIfication;
import com.ysd.entity.ProductSeries;
import com.ysd.util.DBmanager;
public class NewsClassIficationDaoImpl {

	/**
	 * 分页查询所有新闻类别
	 * @return
	 */
	public List<NewsClassiIfication> grtNewsFenLeiListByPage(int page,int size){
		Connection conn = DBmanager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		List<NewsClassiIfication> list = new ArrayList<NewsClassiIfication>();
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from newsclassification order by newsClassIficationId desc limit "+page+","+size);
			while(rs.next()) {
			int	nicId = rs.getInt("newsClassIficationId");
			String nicName = rs.getString("newsClassIficationName");
			NewsClassiIfication nic = new  NewsClassiIfication(nicId,nicName);
			list.add(nic);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBmanager.closeAll(conn, st, rs);
		}
		
	}
	/**
	 * 查询集合长度
	 * @return
	 */
	public List<NewsClassiIfication> grtNewsFenLeiList(){
		Connection conn = DBmanager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		List<NewsClassiIfication> list = new ArrayList<NewsClassiIfication>();
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from newsclassification");
			while(rs.next()) {
			int	nicId = rs.getInt("newsClassIficationId");
			String nicName = rs.getString("newsClassIficationName");
			NewsClassiIfication nic = new  NewsClassiIfication(nicId,nicName);
			list.add(nic);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBmanager.closeAll(conn, st, rs);
		}
		
	}
	
	/**
	 * 
	 * 根据新闻分类id删除分类
	 * nId前台传来的参数
	 * @author 86176
	 *return 返回受影响行数
	 */
	public int deleteNewsCategoryById(int nId) {
		return DBmanager.executeUpdate("DELETE FROM newsclassification where newsClassIficationId ="+nId);
	}
	
	/**
	 * 
	 * 根据新闻分类id批量删除分类
	 * nIds前台传来的参数
	 * @author 86176
	 *return 返回受影响行数
	 */
	public int deleteNewsCategoryByIds(String nIds) {
		String sql = "DELETE FROM newsclassification where newsClassIficationId in("+nIds+")";
		System.out.println(sql);
		return DBmanager.executeUpdate(sql);
	}
	/**
	   *添加新闻分类
	 * nc 前台传来产品分类名称
	 *return 受影响行数
	 */
	public int addNewsCateByName(NewsClassiIfication ns) {
		return DBmanager.executeUpdate("insert into newsclassification VALUES(null,'"+ns.getNewsClassIficationName()+"')");
	}
	
	 /**
	  * 根据传回的name查询是否存在
	  * @param name
	  * @return
	  */
	public List<NewsClassiIfication> grtNewsFenleiByName(String name){
		Connection conn = DBmanager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		List<NewsClassiIfication> list = new ArrayList<NewsClassiIfication>();
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from newsclassification where newsClassIficationName='"+name+"'");
			while(rs.next()) {
			int	nicId = rs.getInt("newsClassIficationId");
			String nicName = rs.getString("newsClassIficationName");
			NewsClassiIfication nic = new  NewsClassiIfication(nicId,nicName);
			list.add(nic);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBmanager.closeAll(conn, st, rs);
		}
		
	}
	/**
	  * 根据传回的id查询数据,为的是数据回显
	  * @param name
	  * @return
	  */
	public NewsClassiIfication grtNewsFenleiById(int ncId){
		Connection conn = DBmanager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		NewsClassiIfication nc=null; 
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from newsclassification where newsClassIficationId="+ncId);
			while(rs.next()) {
			int	nicId = rs.getInt("newsClassIficationId");
			String nicName = rs.getString("newsClassIficationName");
			 nc = new  NewsClassiIfication(nicId,nicName);
			}
			return nc;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBmanager.closeAll(conn, st, rs);
		}
		
	}
	
	/**
	 *编辑新闻分类
	 * nc 前台传来产品分类数据
	 *return 受影响行数
	 */
	public int editNewsCategory(NewsClassiIfication nc) {
		return DBmanager.executeUpdate("update newsclassification set newsClassIficationName = '"+nc.getNewsClassIficationName()+"'  where newsClassIficationId = '"+nc.getNewsClassIficationId()+"' ");
	}
}
